package com.yanqu.road.dao.impl.activity.daomu.pvp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.daomu.pvp.DaoMuPvpPosition;
import com.yanqu.road.entity.activity.daomu.pvp.DaoMuPvpProperty;
import com.yanqu.road.entity.activity.daomu.pvp.DaoMuPvpUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DaoMuPvpUserDataDaoImpl extends TempDataObjectDao<DaoMuPvpUserData> {

    public static String getDaoMuPvpUserDataSql = "select * from `t_c_activity_daomu_pvp_user_data` where `activityId`=?";

    public Map<Long, DaoMuPvpUserData> getDaoMuPvpUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DaoMuPvpUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getDaoMuPvpUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DaoMuPvpUserData userData = parse(rs);
                map.put(userData.getUserId(), userData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDaoMuPvpUserDataSql = "insert into `t_c_activity_daomu_pvp_user_data`(`activityId`,`userId`,`roomId`,`position`,`property`,`roleId`,`apply`,`able`,`out`,`outType`,`outTime`,`pingTime`,`collectionLevel`,`collectionScore`,`pveUploadTime`,`score`,`roundRank`,`roundScore`) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DaoMuPvpUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoomId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPosition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoleId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isApply()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAble()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isOut()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOutType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOutTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPingTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCollectionScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPveUploadTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoundRank())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoundScore())));
        if (getDb().executeNoneQuery(insertDaoMuPvpUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDaoMuPvpUserDataSql = "update `t_c_activity_daomu_pvp_user_data` set `roomId`=?,`position`=?,`property`=?,`roleId`=?,`apply`=?,`able`=?,`out`=?,`outType`=?,`outTime`=?,`pingTime`=?,`collectionLevel`=?,`collectionScore`=?,`pveUploadTime`=?,`score`=?,`roundRank`=?,`roundScore`=? where `activityId`= ? and `userId` = ? ";

    @Override
    protected boolean updateData(DaoMuPvpUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoomId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPosition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRoleId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isApply()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isAble()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isOut()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOutType()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOutTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPingTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCollectionLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCollectionScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPveUploadTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoundRank())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoundScore())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateDaoMuPvpUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DaoMuPvpUserData parseData(ResultSet rs) throws SQLException {
        DaoMuPvpUserData data = new DaoMuPvpUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setRoomId(rs.getInt("roomId"));
        data.setPosition(JSON.parseObject(rs.getString("position"), new TypeReference<DaoMuPvpPosition>() {
        }));
        data.setProperty(JSON.parseObject(rs.getString("property"), new TypeReference<DaoMuPvpProperty>() {
        }));
        data.setRoleId(rs.getInt("roleId"));
        data.setApply(rs.getBoolean("apply"));
        data.setAble(rs.getBoolean("able"));
        data.setOut(rs.getBoolean("out"));
        data.setOutType(rs.getInt("outType"));
        data.setOutTime(rs.getLong("outTime"));
        data.setPingTime(rs.getLong("pingTime"));
        data.setCollectionLevel(rs.getInt("collectionLevel"));
        data.setCollectionScore(rs.getLong("collectionScore"));
        data.setPveUploadTime(rs.getLong("pveUploadTime"));
        data.setScore(rs.getLong("score"));
        data.setRoundRank(JSON.parseObject(rs.getString("roundRank"), new TypeReference<Map<Integer, Integer>>() {
        }));
        data.setRoundScore(JSON.parseObject(rs.getString("roundScore"), new TypeReference<Map<Integer, Long>>() {
        }));
        return data;
    }
}
